package com.share.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.share.domain.*;
import com.share.service.SysRoleMenuService;
import com.share.service.SysRoleService;
import com.share.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: share-admin
 * 描述：
 * @author: XLX
 * @create: 2022-12-04 00:58
 **/
@RequestMapping("/sys/role")
@RestController
public class RoleController extends BaseController {


    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysRoleMenuService sysRoleMenuService;


    @GetMapping("/info/{id}")
    public Result info(@PathVariable(name = "id") Long id){
        SysRole sysRole = sysRoleService.getById(id);
        List<SysRoleMenu> roleMenus = sysRoleMenuService.list(new QueryWrapper<SysRoleMenu>().eq("role_id", id));
        List<Long> menuIds = roleMenus.stream().map(p -> p.getMenuId()).collect(Collectors.toList());
        sysRole.setMenuIds(menuIds);

        return Result.succ(sysRole);
    }


    @GetMapping("/list")
    public Result list(String name){

        Page<SysRole> page = sysRoleService.page(getPage(),
                new QueryWrapper<SysRole>().like(StrUtil.isNotBlank(name),"name",name));

        return Result.succ(page);
    }


    @PostMapping("/save")
    public Result save(@Validated @RequestBody SysRole sysRole){
        sysRole.setCreated(new Date());
        sysRole.setStatu(Const.STATUS_ON);
        sysRoleService.save(sysRole);
        return Result.succ(sysRole);
    }


    @PostMapping("/update")
    public Result update(@Validated @RequestBody SysRole sysRole){
        sysRole.setUpdated(new Date());
        sysRoleService.updateById(sysRole);
        return Result.succ(sysRole);
    }


    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestBody Long[] ids){
        //删除角色
        sysRoleService.removeByIds(Arrays.asList(ids));
        //删除关联的用户信息
       sysUserRoleService.remove(new QueryWrapper<SysUserRole>().in("role_id",ids));

        //删除关联的菜单信息
        sysRoleMenuService.remove(new QueryWrapper<SysRoleMenu>().in("role_id",ids));

        return Result.succ("角色删除成功");

    }


    @PostMapping("/perm/{roleId}")
    public Result rolePerms(@PathVariable("roleId") Long roleId,@RequestBody Long[] menuIds){
        List<SysRoleMenu> sysRoleMenus = new ArrayList<>();

        Arrays.stream(menuIds).forEach(menuId -> {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);

            sysRoleMenus.add(roleMenu);
        });

        // 先删除原来的记录，再保存新的
        sysRoleMenuService.remove(new QueryWrapper<SysRoleMenu>().eq("role_id", roleId));
        sysRoleMenuService.saveBatch(sysRoleMenus);
        return Result.succ(menuIds);

    }


}